﻿/*
 * GLGoodsBase.h
 *
 *  Created on: Jun 16, 2014
 *      Author: yuliang
 */

#ifndef GLGOODSBASE_H_
#define GLGOODSBASE_H_

#include "GLEntityBase.h"
#include "GLGlobalInfo.h"

#include <vector>
#include <map>
#include <string>

/*
 * 有属性的物品,不能重叠,存在唯一id
 */
class CGLGoodsBase : public CGLEntityBase {
public:
	CGLGoodsBase();
	virtual ~CGLGoodsBase();

	virtual inline void Reset() { CGLEntityBase::Reset();
		memset(&m_GoodsInfo, 0, sizeof(m_GoodsInfo));
		m_GoodsFixInfo = nullptr; }
	bool Decode(const std::string& info);
	void Encode(std::string& info);
	inline bool SetGoodsInfo(GoodsInfo& info, const GoodsFixInfo* fix);
	inline GoodsInfo& GetGoodsInfo() { return m_GoodsInfo; }
	inline const GoodsInfo& GetGoodsInfo() const { return m_GoodsInfo; }
	inline const GoodsFixInfo* GetFixInfo() const { return m_GoodsFixInfo; }

	inline void SetExtendInfo(void* info) { SetAttachment(info); }
	inline void* GetExtendInfo() const { return GetAttachment(); }

	inline Type_t GetGoodsType() const { return m_GoodsInfo.m_Type; }
	inline UserId_t GetGoodsId() const { return m_GoodsInfo.m_Id; }
	inline Gold_t GetGoldValue() const { if (nullptr == m_GoodsFixInfo) return -1; return m_GoodsFixInfo->m_GoldValue; }

	inline bool IsTrade() const { return m_GoodsInfo.m_TradeFlag; }
	inline bool IsOverlap() const { if (nullptr == m_GoodsFixInfo) return false; return m_GoodsFixInfo->m_OverlapFlag; }

	inline Number_t GetOverlapMaxNum() const { if (IsOverlap())return GOODS_OVERLAP_MAX_NUM; return GOODS_NOT_OVERLAP_MAX_NUM; }
	inline Number_t GetNums() const { return m_GoodsInfo.m_Number; }
	inline Number_t NeedNums() const { return GetOverlapMaxNum() - m_GoodsInfo.m_Number; }
	inline void AddNums(Number_t nums) { m_GoodsInfo.m_Number += nums; }
	inline void RmNums(Number_t nums) { m_GoodsInfo.m_Number -= nums; }

	inline Value_t GetValues() const { return m_GoodsInfo.m_SurValue; }
	inline void SetValues(Value_t value) { m_GoodsInfo.m_SurValue = value; }
	inline Value_t RmValues(Value_t value);
private:
	const GoodsFixInfo*		m_GoodsFixInfo;
	GoodsInfo				m_GoodsInfo;
};

bool CGLGoodsBase::SetGoodsInfo(GoodsInfo& info, const GoodsFixInfo* fix) {
	if (nullptr == fix) {
		//LOG_ERROR("null|goods|fix|info|type|" << info.m_Type);
		return false;
	}
	m_GoodsFixInfo = fix;
	Number_t tNeed = NeedNums();

	m_GoodsInfo = info;
	if (tNeed < info.m_Number) {
		m_GoodsInfo.m_Number = tNeed;
		info.m_Number -= tNeed;
	} else {
		info.m_Number = 0;
	}
	return GetOverlapMaxNum() >= m_GoodsInfo.m_Number;
}

Value_t CGLGoodsBase::RmValues(Value_t value) {
	if (value >= m_GoodsInfo.m_SurValue) {
		value = m_GoodsInfo.m_SurValue;
		m_GoodsInfo.m_SurValue = 0;
		return value;
	}
	m_GoodsInfo.m_SurValue -= value;
	return value;
}

#endif /* GLGOODSBASE_H_ */
